package com.link.base.base.product.service;

import com.link.base.base.product.dao.mybatis.mapper.MpProdCollMapper;
import com.link.base.base.product.dao.mybatis.mapper.ProdCollMapper;
import com.link.base.base.product.model.ProdColl;
import com.link.base.base.product.model.ProdCollCount;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author unknown
 * @date unknown
 * @comments 产品收藏服务
 */
@Service
public class ProdCollServiceImpl extends BasicServiceImpl<ProdColl> implements ProdCollService {

    @Resource
    private ProdCollMapper prodCollMapper;

    @Resource
    private MpProdCollMapper mpProdCollMapper;

    @Override
    public BasicMapper<ProdColl> getBasicMapper() {
        return prodCollMapper;
    }

    @Override
    @Transactional(rollbackFor = BasicServiceException.class)
    public void insert(ProdColl entity) throws Exception {
        //不分页
        entity.setPageFlag(false);
        //不统计总数
        entity.setTotalFlag(false);
        List<ProdColl> list = new ArrayList<>();
        if (StringUtils.equals(MP,entity.getLoginType())) {
            list = mpProdCollMapper.queryByExamplePage(entity);
        } else {
            list = prodCollMapper.queryByExamplePage(entity);
        }
        if (list.size() == 0) {
            super.insert(entity);
        } else if (list.size() == 1) {
            entity.setId(list.get(0).getId());
        }
    }

    @Override
    public void deleteByProdId(ProdColl entity) throws Exception {
        if (entity.getProdId() == null) {
            throw new ServiceException("ORDER-022");
        }
        if (MP.equals(entity.getLoginType())) {
            mpProdCollMapper.deleteByProdId(entity);
        } else {
            prodCollMapper.deleteByProdId(entity);
        }

    }


    @Override
    public ProdCollCount countProductByEmpId(ProdColl entity) {
        return prodCollMapper.countProductByEmpId(entity);
    }

    @Override
    public void deleteByUPIds(ProdColl pc) {
        prodCollMapper.deleteByUPIds(pc);
    }

    @Override
    public List<ProdCollCount> countProduct(ProdColl entity) throws Exception {
        return prodCollMapper.countProduct(entity);
    }

    @Override
    public List<ProdColl> queryByExamplePage(ProdColl entity) throws Exception {
        //acctId 是存小程序用户的用户id
        String y = "Y";
        if (MP.equals(entity.getLoginType()) || y.equals(entity.getMpUser())) {
            return mpProdCollMapper.queryByExamplePage(entity);
        } else {
            return prodCollMapper.queryByExamplePage(entity);
        }
    }

    @Override
    public ProdColl queryById(ProdColl entity) throws Exception {
        if (MP.equals(entity.getLoginType())) {
            return mpProdCollMapper.queryById(entity);
        } else {
            return prodCollMapper.queryById(entity);
        }
    }

    @Override
    public List<ProdColl> queryByCollExamplePage(ProdColl entity) throws Exception {

        return mpProdCollMapper.queryByCollExamplePage(entity);
    }


}
